//
//  ScenicDescribeVC.m
//  ImGuider
//
//  Created by llt on 2017/4/18.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "ScenicDescribeVC.h"
#import "SDCycleScrollView.h"
#import "ScenicGuiderListCell.h"
#import "ScenicDescribeCell.h"
#import "ScenicPlayVC.h"
#import "NetAPIManager.h"
#import "Scenic.h"
#import "JMHoledView.h"
#import "IGContentTabelViewCell.h"
#import "IGContentTableView.h"
#import "Transition.h"
#import "Masonry.h"
#import "WPYNavBar.h"
#import "GuiderVideosViewController.h"
#import "YUSegment.h"
#import "ScenicStrategyVC.h"
#import "ScenicDetailVC.h"

@interface ScenicDescribeVC () <UITableViewDelegate, UITableViewDataSource,UINavigationControllerDelegate,IGContentTabelViewCellDelegate,WPYNavBarDelegate>

@property (weak, nonatomic) IBOutlet UIView *tableHeadView;
@property (weak, nonatomic) IBOutlet IGContentTableView *tableView;
@property (nonatomic, strong)NSArray *imagesURLStrings;
@property (nonatomic, strong)UIImageView *headerImageView;

@property (nonatomic, strong)IGContentTabelViewCell * contentCell;
@property (nonatomic) BOOL open;
@property (nonatomic, strong) GuiderVideosViewController * guiderVideoVC;
@property (nonatomic, strong) ScenicDetailVC *scenicDetailVC;
@property (nonatomic, strong) ScenicStrategyVC * strategyVC;

@property (nonatomic, strong)WPYNavBar * navBar;

@property (nonatomic, strong)UIView * tabView;

@end

@implementation ScenicDescribeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUp];
    [self addNotification];
    [self loadDataSource];

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.view.frame = [UIScreen mainScreen].bounds;
    self.NoAnimation = NO;
    
    [MobClick beginLogPageView:@"6"];
}


- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    PlayerItem *playItem = [[UIApplication sharedApplication].keyWindow viewWithTag:1000];
    if (!playItem && [PlayerManager shareManeger].isPlay) {
        [[PlayerManager shareManeger] stop];
    }
    [MobClick endLogPageView:@"6"];
}

- (void)dealloc {
    
    [MobClick event:@"6_1_1"];
}
- (void)setUp {
    
    self.barBGAlpha = 0;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.canScroll = YES;
    self.tableHeadView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH /16 * 9.0);
   // self.tableHeadView.backgroundColor = [UIColor redColor];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    // ios 11  scrollvew 的位置调整
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior =UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    [self.tableView registerClass:[IGContentTabelViewCell class] forCellReuseIdentifier:@"IGContentTabelViewCell"];
    self.tableView.showsVerticalScrollIndicator = NO;
    
    _headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH /16 * 9.0)];
    _headerImageView.backgroundColor = [UIColor whiteColor];
    [self.tableHeadView addSubview:_headerImageView];
    self.tableView.tableFooterView = [UIView new];
    [self setNavGradient];
   
}


- (void)loadDataSource {
    
    [ActivityIndicatorView showHUDAddedTo:self.view backgroundColor:kBGColor];
    @weakify(self);
    [NetAPIManager getScenicGuiderListWithScenticID:self.scenic.ID Block:^(Scenic *scenic, NSError *error) {
        @strongify(self);
        [ActivityIndicatorView hideHUDAddedTo:self.view];
        self.scenic = scenic;
        self.title = self.scenic.viewname;
        [self initNavBarWithTips:scenic.tips];
        [self.headerImageView setImageURL:[NSURL URLWithString:[scenic.pictureArray firstObject]] placeholderImage:[UIImage imageNamed:@"placeholder_rec"]];
        [self.guiderVideoVC loadDataSourceWitnScenic:scenic];
        self.scenicDetailVC.scenic = self.scenic;
        [self.scenicDetailVC configInfo];
    
        [self.tableView reloadData];
    }];
}

-(void)initNavBarWithTips:(NSString *)tips {
    
    self.guiderVideoVC  = [[GuiderVideosViewController alloc] init];
    self.guiderVideoVC.superVC = self;
    [self addChildViewController:self.guiderVideoVC];
    self.scenicDetailVC  = [[ScenicDetailVC alloc] init];
    self.scenicDetailVC.scenic = self.scenic;
    [self addChildViewController:self.scenicDetailVC];
    
    if (tips.length > 0) {
        
        self.strategyVC  = [[ScenicStrategyVC alloc] init];
        self.strategyVC.superVC = self;
        [self addChildViewController:self.strategyVC];
        
//        NSString *url = [NSString stringWithFormat:@"%@/%@?platform=ios&lang=%@&country=%@",[AppManager sharedManager].baseURLStr,, [AppManager sharedManager].language, [AppManager sharedManager].currentCountryCode];
        
        self.strategyVC.url = tips;
        [self.navBar AddNavBarArray:@[@"SITELIST_VIDEO",@"SITELIST_SCENICDETAIL",@"SITELIST_STRATEGY"] isBtnLine:YES btnLineColor:kThemeColor];
        
    }else {
        [self.navBar AddNavBarArray:@[@"SITELIST_VIDEO",@"SITELIST_SCENICDETAIL"] isBtnLine:YES btnLineColor:kThemeColor];
    }
    
    [self.tabView addSubview:self.navBar];
    [self.contentCell configData:self.childViewControllers];
    
    
}

- (IGContentTabelViewCell *)contentCell {
    
    if (!_contentCell) {
        _contentCell = [self.tableView dequeueReusableCellWithIdentifier:@"IGContentTabelViewCell"];
        
        _contentCell.delegate = self;
    }
 
    return _contentCell;
}

- (WPYNavBar *)navBar {
    
    if (!_navBar) {
        _navBar = [[WPYNavBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,60) titleColor:kTitleColor andSelectTitleColor:kThemeColor];
        self.navBar.titleColor = kContentColor;
        self.navBar.titleFont = Font(14);
        _navBar.delegate = self;
    }
    
    return _navBar;
}
- (UIView *)tabView {
    
    if (!_tabView) {
        _tabView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,60)];
        _tabView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }
    return _tabView;
}
#pragma mark -UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return  1;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    return self.contentCell;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return self.tabView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 60;
}

#pragma mark -UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    return SCREEN_HEIGHT - 60 - kNavigationBarHeight;
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    
//    
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    
//    if (indexPath.section == 0 && self.scenic.introduction.length > 0) {
//        return;
//    }
//    
//    LineModel *model = self.scenic.lines[indexPath.row];
//    
//    ScenicPlayVC *playVC = [[ScenicPlayVC alloc] init];
//    
//    [AppManager sharedManager].lineid = model.lineid;
//    
//    self.NoAnimation = YES;
//    [self.navigationController pushViewController:playVC animated:YES];;
//    
//}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat yOffset = self.tableView.contentOffset.y;
    CGFloat H = SCREEN_WIDTH/16 * 9.0;
    if (yOffset <= 0) {
        
        CGFloat factor = -yOffset + H ;
        CGRect f = CGRectMake(- (SCREEN_WIDTH * factor/H - SCREEN_WIDTH)/2, yOffset, SCREEN_WIDTH *factor/H, factor);
        self.headerImageView.frame = f;
    }
    
    if (yOffset < H - kNavigationBarHeight  ) {
        
        CGFloat aplha = yOffset /(H -kNavigationBarHeight);
        
        self.barBGAlpha = aplha;
    }else {
        self.barBGAlpha = 1;
    }
    
    //子控制器和主控制器之间的滑动状态切换
    CGFloat tabOffsetY = [_tableView rectForSection:0].origin.y-kNavigationBarHeight;
    

    IGLog(@"%f",tabOffsetY);
    if (scrollView.contentOffset.y > tabOffsetY) {
        scrollView.contentOffset = CGPointMake(0, tabOffsetY);
        if (self.canScroll) {
            
            self.canScroll = NO;
            self.contentCell.canScroll = YES;
        }
    } else {
        if (!self.canScroll) {
            scrollView.contentOffset  = CGPointMake(0, tabOffsetY);
        }
    }
}

#pragma mark -ScenicDescribeCellDelegate
//- (void)describeCell:(ScenicDescribeCell *)cell isShow:(BOOL)show {
//    
//    self.open = show;
//    
//    NSIndexPath *indexpath = [NSIndexPath indexPathForRow:0 inSection:0];
//    [self.tableView reloadRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationFade];
//    
//    if (!show) {
//        self.barBGAlpha = 0;
//        [self.tableView scrollToRowAtIndexPath:indexpath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
//        [MobClick event:@"6_0_1"];
//    } else {
//        
//        [MobClick event:@"6_0_2"];
//    }
//}

- (void)addNotification {
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scrollToTop) name:@"kLeaveTopNtf" object:nil];
}

#pragma mark -- WPYNavBarDelegate

- (void)selectChangetoViewNum:(NSInteger)num {
    
    self.contentCell.selectedIndex = num;
}

#pragma mark -- IGContentTabelViewCellDelegate

- (void)pageChangeToNum:(NSInteger)num {
    
    self.navBar.selectIndex = num;
}

//当滑动下面的PageView时，当前的tableView要禁止滑动
- (void)scrollToBottomWithPosition:(NSString *)position
{
    if ([position isEqualToString:@"ended"]) {
        // pageView不动  tableView 动
        self.tableView.scrollEnabled = YES;
    }else {
        
        // pageView动  tableView 不动
        self.tableView.scrollEnabled = NO;
    }
}
//子控制器到顶部了 主控制器可以滑动
- (void)scrollToTop
{
    self.canScroll = YES;
    self.contentCell.canScroll = NO;
}


- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {
    // _navOperation = operation;
    //在pop和push两种情况分别返回动画过渡代理相应的动画操作
    if (self.NoAnimation) {
        return nil;
    }
    return [Transition transitionWithTransitionType:operation == UINavigationControllerOperationPush ? PageCoverTransitionPush:PageCoverTransitionPop];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
